/**
 * 功能描述
 */
define(function (require, exports, module) {
    //引入视图基类
    var ELView = require("elview");
    var store = require("store");
	var activeUser = store.get("user");

    //assets
    var template = require("./assets/tpl/scattercheckin_main.tpl");
    require("./assets/css/scattercheckin_main.css");
    var processData = require("./store/processData");
    //business

    //component
    var ScatterCheckInSubnav = require("./component/ScatterCheckInSubnav");
    var ScatterCheckInGrid = require("./component/ScatterCheckInGrid");
    var ScatterCheckInEditGrid = require("./component/ScatterCheckInEditGrid");
    var ScatterCheckInForm = require("./component/ScatterCheckInForm");
    var ScatterCheckInDoorForm = require("./component/ScatterCheckInDoorForm");
    
    //constant

    //handler
    var ScatterCheckInHandler = require("./handler/ScatterCheckInHandler");
    var roomPanoramaHandler = require("./handler/roomPanoramaHandler");

    var App = ELView.extend({
        attrs: {
            template: template,
        	model:{
        		flag:"CheckIn"
        	}
        },
        events:roomPanoramaHandler.events,
        changeImg:roomPanoramaHandler.changeImg,
        print:roomPanoramaHandler.print,
        setData:roomPanoramaHandler.setData, //设置房型选择列表data
        renderRoomNumber:roomPanoramaHandler.renderRoomNumber,
        setRoomPersonInfo:roomPanoramaHandler.setRoomPersonInfo,
        roomStateConfirm:roomPanoramaHandler.roomStateConfirm,
        selectPersonInfo : roomPanoramaHandler.selectPersonInfo,
        handler: ScatterCheckInHandler,
        continueAdd:false,
        detailClick:false,//点击目的地，入住价是否可修改
        initComponent: function (params, widget) {
        	ScatterCheckInSubnav.initComponent(params, widget); 
        	ScatterCheckInGrid.initComponent(params, widget); 
        	ScatterCheckInForm.initComponent(params, widget);
        	ScatterCheckInEditGrid.initComponent(params, widget);
        	ScatterCheckInDoorForm.initComponent(params, widget);
        },
        afterInitComponent: function (params, widget) {
            widget.queryByStatus(widget);
        },
        toGrid: function(){
        	this.set("roomRegister",null);
        	this.set("tempGridRowData",null);
        	$(".J-status",this.element).html("");
        	$(".J-room",this.element).html("");
        	this.show([".J-grid"]).hide([".J-form",".J-editgrid",".J-roomTypesPrice",".J-roomStatus",".J-doorForm"]);
			this.get("subnav").hide(["return","save","building","roomType","roomStateReturn","roomStateConfirm"]).show(["registerStatus","search","servicePoint","dateRange", "visit"]);
			this.get("selectRoom").show();
    		this.get("editgrid").setDisabled(false);
    		this.continueAdd = false;
    		this.detailClick = false;
        },
        toCheckIn: function(sdata){
        	
        	this.set("hideForm",".J-doorForm");
    		this.set("showForm",".J-form");
        	
        	if(sdata){
        		var data = processData.cloneObj(sdata);
            	this.set("tempGridRowData",data);
        	}else{
        		this.set("tempGridRowData",data);
        	}
        	this.show([".J-form",".J-editgrid",".J-roomTypesPrice"]).hide([".J-grid",".J-roomStatus",".J-doorForm"]);
			this.get("subnav").show(["return","save"]).hide(["registerStatus","search","servicePoint","dateRange", "visit","building","roomType"]);
			this.get("form").reset();
			$(".J-forms").empty();
			this.set("roomTypeFormData","");
			$(".J-formEnd").empty();
			this.set("formEnd","");
			this.set("roomRegister",null);
        },
        toCheckInDoor: function(sdata){
        	this.set("showForm",".J-doorForm");
    		this.set("hideForm",".J-form");
    		
        	if(sdata){
        		var data = processData.cloneObj(sdata);
            	this.set("tempGridRowData",data);
        	}else{
        		this.set("tempGridRowData",data);
        	}
        	this.show([".J-doorForm",".J-editgrid",".J-roomTypesPrice"]).hide([".J-grid",".J-roomStatus"]);
			this.get("subnav").show(["return","save"]).hide(["registerStatus","search","servicePoint","dateRange", "visit","building","roomType"]);
			$(".J-forms").empty();
			this.set("roomTypeFormData","");
			$(".J-formEnd").empty();
			this.set("formEnd","");
			this.set("roomRegister",null);
			this.get("doorForm").reset();
			this.get("doorForm").setReadonly("checkInDate",false);
			this.get("doorForm").setReadonly("checkOutDate",false);
        },
        toRoomStatus: function(){
        	var that = this;
        	if($(".J-form").hasClass("hidden")){
        		this.set("showForm",".J-doorForm");
        		this.set("hideForm",".J-form");
        	}else{
        		this.set("showForm",".J-form");
        		this.set("hideForm",".J-doorForm");
        	}
        	this.show([".J-roomStatus"]).hide([".J-grid",".J-form",".J-editgrid",".J-roomTypesPrice",".J-doorForm"]);
			this.get("subnav").show(["building","roomType","roomStateReturn","roomStateConfirm"]).hide(["registerStatus","search","servicePoint","dateRange", "visit","return","save"]);
			
			var subnav=this.get("subnav");
			subnav.load("building", {
				params:{
	            	fetchProperties:"pkOrgBuilding,name"
	            },
                callback: function(data) {
                    subnav.setData("building", data);
                    subnav.load("roomType", {
        				params:{
        					orgBuilding: subnav.getValue("building"),
        	            	fetchProperties:"*"
        	            },
                        callback: function(data) {
                            subnav.setData("roomType", data);
                            that.changeImg("clear","1jingfang_chose.png");
                            that.setData("Clear",that);
                        }
                    });
                }
            });
			
			roomPanoramaHandler.showRooms(this);
			
        },
        roomStateReturnClick:function(showForm,hideForm){
        	this.show([showForm,".J-editgrid",".J-roomTypesPrice"]).hide([".J-grid",".J-roomStatus",hideForm]);
			this.get("subnav").show(["return","save"])
				.hide(["registerStatus","search","servicePoint","dateRange", "visit","building","roomType","roomStateReturn","roomStateConfirm"]);
			this.set("roomRegister",null);
        },
        queryByStatus:function(widget){
        	var registerStatus = widget.get("subnav").getValue("registerStatus");
        	widget.get("grid").refresh(null, function (data) {
        		var checkInList=[];
        		var noCheckInList=[];//未登记
        		var noCheckIn={key:"NoCheckIn",value:"待登记"};
        		var checkIn={key:"CheckIn",value:"已登记"};
        		for(var i=0;i<data.length;i++){
        			var status = data[i].roomPersonalInfos;
        			var check = 0;
        			if(status.length){
        				for(var j=0;j<status.length;j++){
        					if(!status[j].checkIn  && status[j].nonMemberRegister.signUpRegister.exitGroup != true){
        						check+= 1;
        						break;
        					}
        				}
        				if(check>0){
        					data[i]["registerStatus"]=noCheckIn;
        					noCheckInList.push(data[i]);
        				}else{
        					data[i]["registerStatus"]=checkIn;
        					checkInList.push(data[i]);
        				}
        			}else{
        				data[i]["registerStatus"]=noCheckIn;
        				noCheckInList.push(data[i]);
        			}   
        		}
        		if(registerStatus=="NoCheckIn"){
            		widget.get("grid").setData(noCheckInList);
        		}else{
            		widget.get("grid").setData(checkInList);
        		}
        	});
        },
    });

    module.exports = App;
});
